//
//  TWSBaseViewController.m
//  TWSWeibo
//
//  Created by Twisal on 2016/12/27.
//  Copyright © 2016年 Twisal. All rights reserved.
//

#import "TWSBaseViewController.h"
#import "TWSVisitorView.h"

@interface TWSBaseViewController () 
@property (nonatomic, strong) UINavigationBar *naviBar;
@property (nonatomic, assign) BOOL isUserLogon;

@end

@implementation TWSBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (void)loadData {
//    [_refreshControl endRefreshing];
}


#pragma mark - 监听事件

/**
 注册
 */
- (void)registerClick {
    NSLog(@"用户注册");
}


/**
 登录
 */
- (void)loginClick {
    NSLog(@"用户登录");
}

#pragma mark - 页面布局
- (void)setupUI {
    
    [self setupNavigationBar];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _isPullup = NO;
    _isUserLogon = false;
    _isUserLogon ? [self setupUserView] : [self setupVisitorView];
    
}

- (void)setupNavigationBar {
    [self.view addSubview:self.naviBar];
    
    self.naviBar.items = @[self.naviItem];
    // 设置背景颜色
    self.naviBar.barTintColor = [UIColor cz_colorWithHex:0xf6f6f6];
    // 设置标题颜色
    self.naviBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor darkGrayColor]};
    // 设置左右两边的按钮字体颜色
    self.naviBar.tintColor = [UIColor orangeColor];
}


/**
 登录后的用户视图
 */
- (void)setupUserView {
    self.view.backgroundColor = [UIColor cz_randomColor];
    [self setupTableView];
    [self loadData];
}


/**
 未登录的访客视图
 */
- (void)setupVisitorView {
    TWSVisitorView *vistorView = [[TWSVisitorView alloc] initWithFrame:self.view.bounds];
    [self.view insertSubview:vistorView belowSubview:self.naviBar];
    vistorView.visitorInfo = self.visitorDictionary;
    [vistorView.registerBtn addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
    [vistorView.loginBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    self.naviItem.leftBarButtonItem = [UIBarButtonItem itemWithTitle:@"注册" target:self action:@selector(registerClick)];
    self.naviItem.rightBarButtonItem = [UIBarButtonItem itemWithTitle:@"登录" target:self action:@selector(loginClick)];
}


/**
 初始化列表视图
 */
- (void)setupTableView {
    // 初始化tableView
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view insertSubview:_tableView belowSubview:self.naviBar];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.contentInset = UIEdgeInsetsMake(self.naviBar.bounds.size.height, 0, self.tabBarController.tabBar.bounds.size.height, 0);
    
    // 初始化下拉刷新条
    _refreshControl = [[UIRefreshControl alloc] init];
    [_refreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];
    [_tableView addSubview:_refreshControl];
    
}


/**
 重写title的Setter方法

 @param title 设置的title
 */
- (void)setTitle:(NSString *)title {
    self.naviItem.title = title;
    self.tabBarItem.title = title;

}


/**
 title的getter方法
 */
- (NSString *)title {
    return self.naviItem.title;
}

#pragma mark - <UITableViewDataSource, UITableViewDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [[UITableViewCell alloc] init];
}

/*
 获取最后一组的最后一行
 */
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 1, 获取当前一组
    NSInteger currentSection = indexPath.section;
    // 2, 获取当前的行
    NSInteger currentRow = indexPath.row;
    if (currentSection < 0 || currentRow < 0) {
        return;
    }
    
    // 3, 获取最后一组
    NSInteger lastSection = [tableView numberOfSections] - 1;
    // 4, 获取最后一组的最后一行
    NSInteger lastSectionRow = [tableView numberOfRowsInSection:lastSection] - 1;
    
    if (currentSection == lastSection && currentRow == lastSectionRow && !_isPullup) {
        NSLog(@"*******上拉刷新");
        _isPullup = YES;
        [self loadData];
    }
    NSLog(@"section---%zd",currentSection);
}

#pragma mark - lazy load
- (UINavigationBar *)naviBar {
    if (_naviBar == nil) {
        _naviBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], 64)];
    }
    return _naviBar;
}

- (UINavigationItem *)naviItem {
    if (_naviItem == nil) {
        _naviItem = [[UINavigationItem alloc] init];
    }
    return _naviItem;
}

@end
